/*
 Copyright (C) 2008  Maurizio Monticelli

 This program is free software; you can redistribute it and/or modify it under the terms of the 
 GNU General Public License as published by the Free Software Foundation; either version 2 of the 
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
 Public License for more details.

 You should have received a copy of the GNU General Public License along with this program; if not, write 
 to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 
 */

package ch.justdoit.andriodrpncalc.model;


import android.widget.EditText;
import ch.justdoit.andriodrpncalc.model.command.CommandProcessor;
import ch.justdoit.andriodrpncalc.view.MainView;
import ch.justdoit.andriodrpncalc.view.OnErrorListener;
import ch.justdoit.andriodrpncalc.view.PromptText;

public class Model {
	public Stack stack;
	public CommandProcessor mCommandManager;
	public IModelObserver mView;
	public Settings mSettings;
	// private StringBuffer prompt;
	private StringBuffer errorMessage;
	private OnErrorListener onErrorListener;

	private static Model instance;
//	private PromptNumber promptNumber;
	private int mCursorPosition = -1;
	private PromptText prompt;


	public synchronized static Model getInstance() {
		if (instance == null) {
			instance = new Model();
		}
		return instance;
	}

	private Model() {
		stack = new Stack(this);
		mCommandManager = new CommandProcessor();
		mSettings = new Settings();
		// prompt = new StringBuffer("");
		errorMessage = new StringBuffer("");
		//promptNumber = new PromptNumber(this);
	}

	public void setView(MainView _view) {
		mView = _view;
	}

//	public String getPrompt() {
//		return promptNumber.getExpressionString();
//	}

//	public PromptNumber getPromptNumber() {
//		return promptNumber;
//	}
//
//	public void setPromptNumber(CalcNumber number) {
//		promptNumber.copy(number);
//	}

//	public void setExpression(String prompt) {
//		// this.promptNumber.setExpressionString(promptNumber.getExpressionString()
//		// + prompt);
//		this.promptNumber.setExpressionString(prompt);
//	}

	public CalcNumber copyPromptNumber() {
		CalcNumber result = null;
		if (prompt.getTokens().size() != 0) {
			result = new CalcNumber(Model.getInstance());
//			result.setExpressionString(prompt.getText().toString());
			result.setToken(prompt.getTokens());
			prompt.resetText();
		}
//		if (!promptNumber.isEmpty()) {
//			result = promptNumber.clone();
//			promptNumber.reset();
//		}
		return result;
	}

	public void enterPromptNumber() {
		CalcNumber number = copyPromptNumber();
		if (number != null) {
			number.parse();
			stack.Put(number);
		}
	}

	public void appendExpression(String text) {
		setErrorMessage("");
		// this.prompt.append(text);
		prompt.addToken(text);
//		this.promptNumber.setExpressionString(promptNumber
//				.getExpressionString()
//				+ text);

	}

	public void setOnErrorListener(OnErrorListener _onErrorListener) {
		onErrorListener = _onErrorListener;
	}

	public String getErrorMessage() {
		return errorMessage.toString();
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = new StringBuffer(errorMessage);
		onErrorListener.onError();
	}

	public void setCursorPosition(int cursorPosition) {
		mCursorPosition = cursorPosition;
	}

	public int getCursorPosition() {
		return mCursorPosition;
	}

	public void setPrompt(PromptText _prompt) {
		prompt = _prompt;
	}
	
	public PromptText getPromptText() {
		return prompt;
	}

	

}
